package cn.com.wxd.servlet;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.com.wxd.service.attach.entity.AttachInfo;
import cn.com.wxd.service.attach.AttachConfEntity;
import cn.com.wxd.service.attach.AttachUtil;
import cn.com.wxd.common.DateUtil;
import cn.com.wxd.common.JsonUtil;
import cn.com.wxd.util.RequestUtil;
import cn.com.wxd.common.file.FileUpload;

/**
 * Title:文件上传 Description:cn.com.wxd.servlet.UploadServlet
 * <p>
 * Company: Amumu管理平台
 * Copyright: Copyright (c) 2015 All right reserved. Created
 * time: 2015-12-25 下午5:47:03
 *
 * @author WangXuDong
 * @version 1.0
 */
public class UploadServlet extends HttpServlet {
    /**
     *
     */
    private static final long serialVersionUID = -3544347703170051001L;
    private static final Logger log = LoggerFactory.getLogger(UploadServlet.class);

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException {

        doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException {

        String param = RequestUtil.getParameter(request, "paramName");
        if (param == null || param.trim().equals("")) {
            log.error("上传参数错误!应在上传地址后添加param参数，该参数在附件上传配置文件中配置。",
                        new Exception(
                                "上传参数错误!应在上传地址后添加param参数，该参数在附件上传配置文件中配置。"));
            return;
        }
        AttachUtil au;
        AttachConfEntity ace;
        try {
            au = AttachUtil.instance();
            ace = au.getAttachConfByParamName(param);
            if (ace == null) {
                throw new Exception("配置文件中没有找到该param的配置！");
            }
        } catch (Exception e) {
            log.error("param参数错误，该参数不存在!", e);
            return;
        }
        FileUpload ful = new FileUpload();
        List<AttachInfo> list = null;
        try {
            list = ful.fileUpload(request,
                    ace.getPath() + File.separator + DateUtil.getDatetimeStr("yyyyMMdd"),
                    ace.getPath() + File.separator + "temp", ace.isRecorddb(), ace);
            request.setCharacterEncoding("UTF-8");
        } catch (IOException e) {
            log.error("上传错误" + e.getMessage(), e);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        response.setContentType("text/html; charset=UTF-8");
        PrintWriter out = null;
        try {
            out = response.getWriter();
        } catch (IOException e) {
            log.error(e.getMessage());
        }
        String json = JsonUtil.objectToJson(list);
        if (out != null) {
            out.print(json);
            out.flush();
            out.close();
        }
    }
}
